package com.jumi.microservice.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.MessageMail;
import com.jumi.microservice.domain.MessageTemplate;
import com.jumi.microservice.domain.vo.MessageUnreadVO;
import com.jumi.microservice.domain.vo.MessageVO;
import com.jumi.microservice.jwt.constant.JwtAdminInfo;
import com.jumi.microservice.mapper.MessageTemplateMapper;
import com.jumi.microservice.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "C-消息提醒相关接口")
@RequestMapping("/api/message/Message")
public class MessageController {

    @Resource
    private MessageService messageService;
    @Resource
    private MessageTemplateMapper messageTemplateMapper;


    @ApiOperation(value = "获取用户消息中心未读数量", notes = "获取用户消息中心未读数量")
    @GetMapping("/unreadMessageNumber")
    public ResponseResult<MessageUnreadVO> unreadMessageNumber(HttpServletRequest request){
        Integer apiUserId = request.getIntHeader(JwtAdminInfo.apiUserId);
        MessageUnreadVO vo = messageService.SystemNewNum(apiUserId.longValue());
        return ResultGenerator.genSuccessResult(vo);
    }

    @ApiOperation(value = "获取消息类型列表", notes = "获取消息类型列表")
    @ApiImplicitParam(name = "type", value = "1：交易物流通知,2：商品活动通知,3：系统消息通知,4：余额变动通知")
    @GetMapping("/messageList")
    public BasePageResponse<List<MessageVO>> messageList(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "0") Integer size,
            String type,
            HttpServletRequest request){
        Integer apiUserId = request.getIntHeader(JwtAdminInfo.apiUserId);
        LambdaQueryWrapper<MessageMail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MessageMail::getTemplateTitleType,type);
        wrapper.eq(MessageMail::getReMemberId,apiUserId.longValue());
        wrapper.eq(MessageMail::getIsDel,0);
        wrapper.orderByDesc(MessageMail::getMessageAddTime);
        IPage<MessageMail> pageInfo = messageService.page(new Page<>(page, size), wrapper);

        for (MessageMail messageMail : pageInfo.getRecords()){
            MessageTemplate messageTemplate = messageTemplateMapper.selectById(messageMail.getTemplateId());
            messageMail.setSmtName(messageTemplate.getSmtName());
        }

        return ResultGenerator.genSuccessResultPage(pos2Vos(pageInfo.getRecords()), pageInfo.getTotal());
    }

    @ApiOperation(value = "获取消息内容", notes = "获取消息内容")
    @ApiImplicitParam(name = "messageId", value = "消息id")
    @GetMapping("/details")
    public ResponseResult<MessageMail> details(Long messageId){
        MessageMail messageMail = messageService.selectMessageId(messageId);
        return ResultGenerator.genSuccessResult(messageMail);
    }


    @ApiOperation(value = "阅读消息", notes = "阅读消息")
    @ApiImplicitParam(name = "messageId", value = "消息id")
    @GetMapping("/read")
    public ResponseResult<Boolean> read(Long messageId){
        messageService.readMessage(messageId);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }


    /**
     * do->vo
     * @param messageMails
     * @return
     */
    private List<MessageVO> pos2Vos(List<MessageMail> messageMails) {
        List<MessageVO> pos = messageMails.stream().map(p -> {
            MessageVO vo = null;
            try {
                vo = p.clone(MessageVO.class);
            } catch (Exception e) {
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
        return pos;
    }

}
